module.exports = (app) => {
  const { STRING, TEXT, INTEGER, DATE, BOOLEAN } = app.Sequelize

  const Works = app.model.define('works', {
    id: {
      type: INTEGER(20).UNSIGNED,
      primaryKey: true,
      autoIncrement: true,
      allowNull: false,
      comment: '留言墙id'
    },
    username: {
      type: STRING(20),
      allowNull: false,
      comment: '留言墙用户名'
    },
    message: {
      type: TEXT,
      allowNull: false,
      comment: '留言墙内容'
    },
    color_type: {
      type: INTEGER(20),
      allowNull: false,
      defaultValue: 1,
      comment: '留言墙颜色类型'
    },
    tag: {
      type: STRING(20),
      allowNull: false,
      comment: '留言墙标签'
    },
    user_id: {
      type: STRING(50),
      allowNull: false,
      defaultValue: 1,
      comment: '发表人id'
    },
    status: {
      type: BOOLEAN,
      allowNull: false,
      defaultValue: 1,
      comment: '留言墙状态'
    },
    created_at: {
      type: DATE,
      get() {
        return new Date(this.getDataValue('created_at')).getTime()
      }
    },
    updated_at: DATE
  })
  // 设置关联关系
  Works.associate = function () {
    Works.hasMany(app.model.WorkComment, {
      foreignKey: 'works_id',
      as: 'comments'
    })
    Works.hasMany(app.model.WorksLike, {
      foreignKey: 'works_id',
      as: 'likes'
    })
  }
  return Works
}
